package com.yx.cloud.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;

import com.bumptech.glide.Glide;
import com.iot.common.utils.LogUtils;
import com.iot.sweeper.AppContext;
import com.yx.cloud.AppStatusConstant;
import com.yx.cloud.AppStatusManager;
import com.yx.cloud.R;
import com.yx.cloud.constant.Constants;
import com.yx.cloud.databinding.WelcomeuiActivityBinding;
import com.yx.cloud.utils.Intent_utils;
import com.yx.cloud.utils.LanguageUtils;
import com.yx.cloud.utils.SharedPrefsUtil;

/**
 * 创建时间 : 2017/9/23.
 * 创建人 :
 * 功能描述 :欢迎界面
 */

public class WelcomeUI extends Activity {
    private static final String TAG = "WelcomeUI";
    private boolean mIsFirstRun;
    private WelcomeuiActivityBinding binding;
    private boolean isLogin;
    private long waitTime=5000/*Long.parseLong(BuildConfig.MILLISINFUTURE)*/;
    /**
     * 倒计时线程
     */
    CountDownTimer timer = new CountDownTimer(waitTime, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            // LogUtils.i("millisInFuture : " + millisUntilFinished);
                binding.tvTiming.setText((millisUntilFinished / 1000) + 1 + "");
        }

        @Override
        public void onFinish() {
            Log.i(TAG, "WelcomeUI--isLogin--jjjjjjjjjjjjjjjj> " + mIsFirstRun);
            activityJump();
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        isLogin = AppContext.getAccountService().isLogin();
        LogUtils.i("islogin: "+isLogin);
        //如果是第二次启动 并且 应用没有被杀掉 那么直接关闭界面 到上一次保存的界面去
        if (!isTaskRoot() && AppStatusManager.getInstance().getAppStatus() != AppStatusConstant.STATUS_FORCE_KILLED) {
            //解决每次点击桌面图标都会重新启动问题
            finish();
        } else {
            binding = WelcomeuiActivityBinding.inflate(getLayoutInflater());
            Window window = getWindow();
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
            window.setNavigationBarColor(Color.TRANSPARENT);
            setContentView(binding.getRoot());

            LanguageUtils.getAndsetLanguage(this, "welcome");
            //判断是否是第一次运行程序
            mIsFirstRun = SharedPrefsUtil.getValue(this, "isFirstRun", "isFirstRun", false);


            binding.tvTimingLeapfrog.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    timer.cancel();
                    activityJump();
                }
            });
            binding.llWelcomeJump.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    timer.cancel();
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            Intent_utils.gotoShop(WelcomeUI.this, "http://isemall.cn/eEryu6jac");
                        }
                    }).start();
                    activityJump();
                }
            });
            Glide.with(this).load(R.drawable.welcome).into(binding.ivAdvertise);
            timer.start();

        }
        AppStatusManager.getInstance().setAppStatus(AppStatusConstant.STATUS_NORMAL); //进入应用初始化设置成正常状态
    }

    private void activityJump() {
        Intent intent;
        if (isLogin) {
            intent = new Intent(WelcomeUI.this, MainBottomTabActivity.class);
        } else {
            intent = new Intent(WelcomeUI.this, LoginActivity.class);
        }
        startActivity(intent);
        finish();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        //禁止划屏关闭界面
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (timer != null) {
            timer.cancel();
        }
    }
}
